package cn.csc.module.member.controller.app.menu;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.member.controller.app.menu.vo.*;
import cn.csc.module.member.convert.menu.MemberMenuConvert;
import cn.csc.module.member.dal.dataobject.menu.MemberMenuDO;
import cn.csc.module.member.service.menu.MemberMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "用户 APP - 菜单权限")
@RestController
@RequestMapping("/member/menu")
@Validated
public class AppMemberMenuController {

    @Resource
    private MemberMenuService menuService;

    @PostMapping("/create")
    @ApiOperation("创建菜单权限")

    public CommonResult<BigInteger> createMenu(@Valid @RequestBody AppMemberMenuCreateReqVO createReqVO) {
        return success(menuService.createMenu(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新菜单权限")

    public CommonResult<Boolean> updateMenu(@Valid @RequestBody AppMemberMenuUpdateReqVO updateReqVO) {
        menuService.updateMenu(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除菜单权限")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)

    public CommonResult<Boolean> deleteMenu(@RequestParam("id") BigInteger id) {
        menuService.deleteMenu(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得菜单权限")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)

    public CommonResult<AppMemberMenuRespVO> getMenu(@RequestParam("id") BigInteger id) {
        MemberMenuDO menu = menuService.getMenu(id);
        return success(MemberMenuConvert.INSTANCE.convert(menu));
    }

    @GetMapping("/list")
    @ApiOperation("获得菜单权限列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)

    public CommonResult<List<AppMemberMenuRespVO>> getMenuList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<MemberMenuDO> list = menuService.getMenuList(ids);
        return success(MemberMenuConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得菜单权限分页")

    public CommonResult<PageResult<AppMemberMenuRespVO>> getMenuPage(@Valid AppMemberMenuPageReqVO pageVO) {
        PageResult<MemberMenuDO> pageResult = menuService.getMenuPage(pageVO);
        return success(MemberMenuConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出菜单权限 Excel")

    @OperateLog(type = EXPORT)
    public void exportMenuExcel(@Valid AppMemberMenuExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MemberMenuDO> list = menuService.getMenuList(exportReqVO);
        // 导出 Excel
        List<AppMemberMenuExcelVO> datas = MemberMenuConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "菜单权限.xls", "数据", AppMemberMenuExcelVO.class, datas);
    }

}
